package com.mxc.website.websiteService.impl;

import cn.hutool.core.lang.UUID;
import com.mxc.website.model.Customers;
import com.mxc.website.model.QuestionLibrary;
import com.mxc.website.websiteService.WebsiteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wendongdong
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WebsiteServiceImpl implements WebsiteService {

    private final MongoTemplate mongoTemplate;

    @Override
    public Customers save(Customers customers) {
        Customers save = mongoTemplate.save(customers);
        log.info("save:{}", save);
        return save;
    }

    @Override
    public List<QuestionLibrary> getQuestionLibrary(String position) {
        Query query = new Query();
        query.addCriteria(Criteria.where("position").is(position));
        query.addCriteria(Criteria.where("type").is("q"));
        query.addCriteria(Criteria.where("pid").is("0"));
        return mongoTemplate.find(query, QuestionLibrary.class);
    }

    @Override
    public List<QuestionLibrary> getQuestionLibraryByPid(String pid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("pid").is(pid));
        return mongoTemplate.find(query, QuestionLibrary.class);
    }

    @Override
    public List<QuestionLibrary> getQuestionLibraryByText(String text) {
        // 创建一个不区分大小写的正则表达式，匹配包含 keyword 的字符串
        String regex = ".*" + text + ".*";
        // 创建查询条件，使用正则表达式并添加不区分大小写的标志
        Criteria criteria = Criteria.where("question").regex(regex, "i");
        // 创建查询对象
        Query query = new Query(criteria);
        // 执行查询
        return mongoTemplate.find(query, QuestionLibrary.class);
    }
}
